#include "utils.h"

using namespace std;

bool get(istream &stream, BYTE &b) {
	int i = stream.get();
	if (stream.eof())
		return false;
	b = i;
	return true;
};

bool put(ostream &stream, const BYTE &b) {
	stream.put(b);
	return stream.good();
};

bool get(istream &stream, WORD &w) {
	BYTE low,high;
	if (!get(stream,low))
		return false;
	if (!get(stream,high))
		return false;
	w = (WORD(high)<<8) | WORD(low);
	return true;
};

bool put(ostream &stream, const WORD &w) {
	BYTE low = w & 0x00ff;
	BYTE high = w >> 8;
	if (!put(stream,low))
		return false;
	if (!put(stream,high))
		return false;
	return true;
};

bool get(istream &stream, DWORD &dw) {
	WORD low,high;
	if (!get(stream,low))
		return false;
	if (!get(stream,high))
		return false;
	dw = (DWORD(high)<<16) | DWORD(low);
	return true;
};

bool put(ostream &stream, const DWORD &dw) {
	WORD low = dw & 0x0000ffff;
	WORD high = dw >> 16;
	if (!put(stream,low))
		return false;
	if (!put(stream,high))
		return false;
	return true;
};

bool get(istream &stream, LONG &l) {
	DWORD dw;
	if (!get(stream,dw))
		return false;
	l = dw;
	return true;
};

bool put(ostream &stream, const LONG &l) {
	DWORD dw = l;
	if (!put(stream,dw))
		return false;
	return true;
};
